define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pt/depart/index' + location.search,
                    add_url: 'pt/depart/add',
                    edit_url: 'pt/depart/edit',
                    // del_url: 'pt/depart/del',
                    multi_url: 'pt/depart/multi',
                    import_url: 'pt/depart/import',
                    table: 'pt_depart',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Depart_name'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"hidden":__('Status hidden'),"normal":__('Status normal')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                          field: 'operate', 
                          title: __('Operate'), 
                          table: table, 
                          events: Table.api.events.operate, 
                          buttons: [
                              {
                                  name: 'planyear',
                                  text: __('年月计划'),
                                  icon: 'fa',
                                  classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                  url: 'pt/depart/year'
                              },
                              {
                                  name: 'planrange',
                                  text: __('区间计划'),
                                  icon: 'fa',
                                  classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                  url: 'pt/depart/range'
                              },
                          ],

                          formatter: Table.api.formatter.operate

                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                commonSearch: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        year: function () {
            // 初始化表格参数配置
            var ids = window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1);
            Table.api.init({
                extend: {
                    index_url: 'pt/depart/year/ids/' + ids + location.search,
                    add_url: 'pt/depart/yearadd/ids/' + ids,
                    // edit_url: 'pt/depart/yearedit',
                    // del_url: 'pt/depart/del',
                    multi_url: 'pt/depart/multi',
                    import_url: 'pt/depart/import',
                    table: 'pt_depart',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'year_id',
                sortName: 'year_id',
                escape: false,
                pagination:false,  
                columns: [
                    [
                        {field: 'year', title: __('年份'), operate: false},
                        {field: 'month_1', title: __('1月'), operate: false},
                        {field: 'month_2', title: __('2月'), operate: false},
                        {field: 'month_3', title: __('3月'), operate: false},
                        {field: 'month_4', title: __('4月'), operate: false},
                        {field: 'month_5', title: __('5月'), operate: false},
                        {field: 'month_6', title: __('6月'), operate: false},
                        {field: 'month_7', title: __('7月'), operate: false},
                        {field: 'month_8', title: __('8月'), operate: false},
                        {field: 'month_9', title: __('9月'), operate: false},
                        {field: 'month_10', title: __('10月'), operate: false},
                        {field: 'month_11', title: __('11月'), operate: false},
                        {field: 'month_12', title: __('12月'), operate: false},
                        {field: 'total_fee', title: __('年度总费用'), operate: false},
                        {
                          field: 'operate', 
                          title: __('Operate'), 
                          table: table, 
                          events: Table.api.events.operate, 
                          buttons: [
                              {
                                  name: 'planyear',
                                  text: __('编辑计划'),
                                  icon: 'fa',
                                  classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                  url: 'pt/depart/yearedit/departId/' + ids,
                              },
                          ],

                          formatter: Table.api.formatter.operate

                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                commonSearch: false,
                search: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        yearadd: function () {
            Controller.api.bindevent();
        },
        yearedit: function () {
            Controller.api.bindevent();
        },
        range: function () {
            // 初始化表格参数配置
            var ids = window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1);
            Table.api.init({
                extend: {
                    index_url: 'pt/depart/range/ids/' + ids + location.search,
                    add_url: 'pt/depart/rangeadd/ids/' + ids,
                    // edit_url: 'pt/depart/yearedit',
                    // del_url: 'pt/depart/del',
                    multi_url: 'pt/depart/multi',
                    import_url: 'pt/depart/import',
                    table: 'pt_depart',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                escape: false,
                columns: [
                    [
                        {field: 'id', title: __('ID'), operate: false},
                        {field: 'date_str', title: __('日期'), operate: false},
                        {field: 'plan_total', title: __('计划人次需求'), operate: false},
                        {field: 'consume_total', title: __('已经消耗人次'), operate: false},
                        {field: 'remain_fee', title: __('剩余费用（元）'), operate: false,
                          formatter: function(value, row, index) {
                            if (row.remain_fee == 0) {
                                return '未计算';
                            }
                            return value;
                          }
                        },
                        {field: 'remain_percent', title: __('剩余费用占比'), operate: false,
                          formatter: function(value, row, index) {
                            if (row.remain_percent == 0) {
                                return '未计算';
                            }
                            return value + '%';
                          }
                        },
                        {
                          field: 'operate', 
                          title: __('Operate'), 
                          table: table, 
                          events: Table.api.events.operate, 
                          buttons: [
                              {
                                  name: 'rangeedit',
                                  text: __('编辑计划'),
                                  icon: 'fa',
                                  classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                  url: 'pt/depart/rangeedit',
                              },
                          ],

                          formatter: Table.api.formatter.operate

                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                commonSearch: false,
                search: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        rangeadd: function () {
            Controller.api.bindevent();
        },
        rangeedit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
